package jp.ac.u_tokyo.dch.action.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.model.db.User;
import jp.ac.u_tokyo.dch.model.db.UserGroup;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.service.Transaction;
import jp.ac.u_tokyo.dch.service.UserService;
import jp.ac.u_tokyo.dch.util.DchCommonUtil;
import jp.ac.u_tokyo.dch.util.StringCheckUtil;
import jp.ac.u_tokyo.dch.util.common.ModoruUtil;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/**
*
* @version 1.1
*
*/
public class UserAction extends ActionSupport implements DaoServiceAware {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;

	private List<User> userList;

	private User userView;

	private List<UserGroup> groupInfoList;

	/** 画面から入力されたユーザグループ情報*/
	private List<String> userGroupList;
	/**
	 * タイプ
	 */
	private String actionType;

	/**
	 * 編集、削除ID
	 */
	private String editId;

	/**
	 * 検索情報
	 */
	private String searchUserid;
	private String searchUserName;
	private String searchUserGroupId;
	private String searchUserGroupName;

	public String index() throws Exception {
		return SUCCESS;
	}

	/**
	 *
	 * @return
	 * @throws Exception
	 */
	public String manage() throws Exception {

		ModoruUtil.modoruProcess(ServletActionContext.getRequest(), "/users/user-manage", ServletActionContext.getRequest().getQueryString());

		UserService userService = new UserService(dbConnection);
		userList = userService.selectUsers();
		return SUCCESS;
	}

	public String search() throws Exception {

		UserService userService = new UserService(dbConnection);
		userList = userService.searchUsers(searchUserid, searchUserName, searchUserGroupId, searchUserGroupName);

		return SUCCESS;
	}

	/**
	 *
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {

		actionType = Constants.ADD;

		userView = new User();
		makeUserGroupList();

		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		String currentDate =DchCommonUtil.getCurrentDate();
		userView.setCreateUser(fUserId);
		userView.setCreateDate(currentDate);
		userView.setUpdateUser(fUserId);
		userView.setUpdateDate(currentDate);
		userView.setFrontLevel("1");
		userView.setManageLevel("1");

		return SUCCESS;
	}

	/**
	 * 編集初期化
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String editInit() throws Exception {

		actionType = Constants.MOD;

		UserService service = new UserService(dbConnection);
		userView = service.selectUser(editId);
		makeUserGroupList();
		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		String currentDate =DchCommonUtil.getCurrentDate();
		userView.setUpdateUser(fUserId);
		userView.setUpdateDate(currentDate);
		return SUCCESS;
	}
	/**
	 * 削除する
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String delInit() throws Exception {

		actionType = Constants.DEL;

		UserService service = new UserService(dbConnection);
		userView = service.selectUser(editId);
		makeUserGroupList();
		return SUCCESS;
	}

	/**
	 *
	 * @return
	 * @throws Exception
	 */
	public String modoru() throws Exception {

		userView.setGroupList(userGroupList);
		makeUserGroupList();
		return SUCCESS;
	}

	/**
	 * 確認する処理を行う
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String confirm() throws Exception {

		UserService service = new UserService(dbConnection);

		userView.setGroupList(userGroupList);
		makeUserGroupList();

		String id = userView.getId();
		String name = userView.getName();
		String password = userView.getPassword();

		if (StringUtils.isEmpty(id)) {
			addFieldError("userView.id", getText("requiredstring", new String[] { "ユーザID" }));
			return ERROR;
		}

		if (StringUtils.isEmpty(name)) {
			addFieldError("userView.name", getText("requiredstring", new String[] { "ユーザ名" }));
			return ERROR;
		}

		if (StringUtils.isEmpty(password)) {
			addFieldError("userView.password", getText("requiredstring", new String[] { "パスワード" }));
			return ERROR;
		}

		//英数字以外の場合
		if (StringCheckUtil.checkZenkaku(id)) {
			addFieldError("id", getText("hannkakuCheck", new String[] { "ユーザID" }));
			return ERROR;
		}

		//英数字以外の場合
		if (StringCheckUtil.checkZenkaku(password)) {
			addFieldError("id", getText("hannkakuCheck", new String[] { "パスワード" }));
			return ERROR;
		}

		if (Constants.ADD.equals(actionType) && service.isExistsId(id)) {

			addFieldError("id", getText("duplicateMsgArgs", new String[] { "ユーザID" }));
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 登録、削除、編集処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	@Transaction
	public String execute() throws Exception {

		userView.setGroupList(userGroupList);

		UserService service = new UserService(dbConnection);

		if (Constants.ADD.equals(actionType)) {
			service.insert(userView);

		} else if (Constants.MOD.equals(actionType)) {
			service.update(userView);

		} else if (Constants.DEL.equals(actionType)) {
			service.delete(userView);
		}
		return SUCCESS;
	}

	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;
	}

	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public List<UserGroup> getGroupInfoList() {
		return groupInfoList;
	}

	public void setGroupInfoList(List<UserGroup> groupInfoList) {
		this.groupInfoList = groupInfoList;
	}

	public String getActionType() {
		return actionType;
	}
	public void setActionType(String actionType) {
		this.actionType = actionType;
	}


	public String getEditId() {
		return editId;
	}

	public void setEditId(String editId) {
		this.editId = editId;
	}

	private void makeUserGroupList() throws DBException {

		UserService userService = new UserService(dbConnection);

		groupInfoList = userService.selectUserGroup();

		Map<String, String> groupInfoMap = new HashMap<String, String>();

		if (userView.getGroupList() != null) {
			for (String id : userView.getGroupList()) {
				groupInfoMap.put(id, id);
			}
		}

		userGroupList = new ArrayList<String>();

		for (UserGroup userGroup : groupInfoList) {

			if (groupInfoMap.containsKey(userGroup.getId())) {

				userGroupList.add("1");

			} else {

				userGroupList.add("0");
			}
		}
	}

	public List<String> getUserGroupList() {
		return userGroupList;
	}

	public void setUserGroupList(List<String> userGroupList) {
		this.userGroupList = userGroupList;
	}

	public User getUserView() {
		return userView;
	}

	public void setUserView(User userView) {
		this.userView = userView;
	}

	public String getSearchUserid() {
		return searchUserid;
	}

	public void setSearchUserid(String searchUserid) {
		this.searchUserid = searchUserid;
	}

	public String getSearchUserName() {
		return searchUserName;
	}

	public void setSearchUserName(String searchUserName) {
		this.searchUserName = searchUserName;
	}

	public String getSearchUserGroupId() {
		return searchUserGroupId;
	}

	public void setSearchUserGroupId(String searchUserGroupId) {
		this.searchUserGroupId = searchUserGroupId;
	}

	public String getSearchUserGroupName() {
		return searchUserGroupName;
	}

	public void setSearchUserGroupName(String searchUserGroupName) {
		this.searchUserGroupName = searchUserGroupName;
	}

}
